################################################################################
# Title   : Replication Code — Union Spillovers Paper
# Project : The Impact of Unions on Non-union Wage Setting: Threats and Bargaining
# Authors : David A. Green, Ben M. Sand, Iain G. Snoddy, Jeanne Tschopp
# Output  : Figures 4
# Date    : August 2025
################################################################################

# Set default graphical theme
theme_set(theme_light())

# Create output directory if it doesn't exist
if (!dir.exists(figs)) dir.create(figs, recursive = TRUE)

# Load data
dat <- read_dta(paste0(wd,"/decomp_grp_all_case_6.dta")) %>% 
  mutate(year = case_when(
    year2 ==  1 ~ "1980",
    year2 ==  2 ~ "1990",
    year2 ==  3 ~ "2000",
    year2 ==  4 ~ "2010")) 

dat <- dat %>% 
  # select(year, observed, cf1, cf2, cf3  ) %>% 
  mutate(year = as.Date(ISOdate(year, 1, 1)))

data <- dat

# Transform data into long format
data_long <- data %>%
  pivot_longer(
    cols = c(observed, cf1, cf2, cf3),
    names_to = "Category",            
    values_to = "Value"               
  ) %>% 
  mutate(Category = factor(Category, levels = c("observed", "cf1", "cf2", "cf3")))

# Plot the data
ggplot(data_long, aes(x = year, 
                      y = Value, 
                      color = Category, 
                      shape = Category, 
                      linetype = Category)) +
  geom_point(size=2) +
  geom_line(size=1) +
  labs(
    y = "Change in Log Wage",
    x = "Year"
  ) +
  scale_color_discrete(name = "Category", 
                       labels = c(
                         TeX("Observed ($\\Delta w_{ct}$)"), 
                         TeX("Fixed Union Prop. ($\\Delta w_{ct}^{cf1}$)"), 
                         TeX("$\\Delta w_{ct}^{cf1} +$  Fixed Premium ($\\Delta w_{ct}^{cf2}$)"), 
                         TeX("Full Counterfactual ($\\Delta w_{ct}^{cf3}$)")
                       ))+ 
  scale_linetype_discrete(name = "Category",
                          labels = c(
                            TeX("Observed ($\\Delta w_{ct}$)"), 
                            TeX("Fixed Union Prop. ($\\Delta w_{ct}^{cf1}$)"), 
                            TeX("$\\Delta w_{ct}^{cf1} +$  Fixed Premium ($\\Delta w_{ct}^{cf2}$)"), 
                            TeX("Full Counterfactual ($\\Delta w_{ct}^{cf3}$)")
                          ))+  
  scale_shape_manual(values = c("observed" = 16, "cf1" = 17, "cf2" = 1, "cf3" = 2),
                     name = "Category", 
                     labels = c(
                       TeX("Observed ($\\Delta w_{ct}$)"), 
                       TeX("Fixed Union Prop. ($\\Delta w_{ct}^{cf1}$)"), 
                       TeX("$\\Delta w_{ct}^{cf1} +$  Fixed Premium ($\\Delta w_{ct}^{cf2}$)"), 
                       TeX("Full Counterfactual ($\\Delta w_{ct}^{cf3}$)")
                     )) +
  
  scale_x_date(
    expand = c(0.01, .01), 
    date_labels = "%Y"
  ) +
  theme_minimal() +
  theme(
    legend.position = c(1, 1),  # Position legend inside the plot (top-right)
    legend.justification = c(1, 1),  # Align the legend to the top-right corner
    legend.background = element_rect(fill = "white", color = "black"),  # Add background to legend
    legend.title = element_blank(),  # Remove legend title
    legend.text = element_text(size = 12),  # Adjust legend text size
    legend.key.width = unit(2, "lines"),
    legend.key.height = unit(1, "lines"),
  ) +
  
  # Adjust the guides to have 2 rows and 2 columns
  guides(
    linetype = guide_legend(nrow = 4, ncol = 1, byrow = TRUE),
    colour = guide_legend(nrow = 4, ncol = 1, byrow = TRUE),
    shape = guide_legend(nrow = 4, ncol = , byrow = TRUE)
  ) -> p1

p1

ggsave(paste0(figs,"/Figure_4.pdf"),
       units = c("in"),
       width = 8, height = 4.5)